/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.enterprise.dao.PlantDao;
import org.crystalrange.enterprise.model.Plant;
import org.crystalrange.enterprise.service.PlantService;
import org.crystalrange.model.User;
import org.crystalrange.service.UserService;

public class PlantServiceImpl implements PlantService {

    private PlantDao plantDao;
    private UserService userService;

    public void setPlantDao(PlantDao plantDao) {
        this.plantDao = plantDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public Plant create(String username) {
        
        User currentUser = this.userService.findByUsername(username);  
        
        Plant plant = new Plant();
        plant.setCreatedBy(currentUser);
        plant.setCreatedDate(new Date());
        
        return plant;
    }
    
    public void save(Plant plant, String username) {
        
        User currentUser = this.userService.findByUsername(username);  
        
        plant.setUpdatedBy(currentUser);
        plant.setUpdatedDate(new Date());
        
        this.plantDao.save(plant);
    }

    public void remove(Plant plant) {
        this.plantDao.remove(plant);
    }

    public Plant find(Long id) {
        return this.plantDao.find(id);
    }

    public List<Plant> findAll() {
        return this.plantDao.findAll();
    }
    
    public SearchResult<Plant> searchAndCount(Search search) {
        return this.plantDao.searchAndCount(search);
    }

    public List<Plant> search(Search search) {
        return this.plantDao.search(search);
    }
    
    public Plant searchUnique(Search search) {
        return this.plantDao.searchUnique(search);
    }
}
